package com.dwm716.shorturl.pagehelper;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author crg
 * @date 2019/05/30
 */
@Data
public class PageResult<T> implements Serializable {

    /**
     * 总条数
     */
    private long totalCount;

    /**
     * 总页数
     */
    private int pageCount;

    /**
     * 数据
     */
    private List<T> data;

    /**
     * 当前页数
     */
    private int pageNo = 1;
    /**
     * 每页条数
     */
    private int pageSize = 20;

    public static <T> PageResult<T> create(List<T> data,long count,int pageSize) {
        PageResult<T> result = new PageResult<>();
        result.setData(data);
        result.setTotalCount(count);
        result.setPageSize(pageSize);
        result.setPageCount(result.getTotalPage());
        return result;
    }

    private int getTotalPage() {
        pageCount= (int) ((totalCount%pageSize==0)?totalCount/pageSize:totalCount/pageSize+1);
        return pageCount;
    }

    //返回-1则表示无需分页
    public static Integer pageNo(Integer pageNo, Integer pageSize){
       if(null == pageNo || null == pageSize){
           return -1;
       }
        if (pageNo > 0) {
            return (pageNo - 1) * pageSize(pageSize);
        }
        return pageNo;
    }

    //返回-1则表示无需分页
    public static Integer pageSize(Integer pageSize){
        if(null == pageSize){
            return -1;
        }
        if (pageSize > 1000) {
            return 1000;
        }
        return pageSize;
    }
}
